﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GameEngine.Managers.IManagers;
using GameEngine.Scenes;
using GameEngine.Camera;
using GameEngine.Managers.ConcreteManagers;

namespace GameEngine
{
    public class GameEngine
    {
        private IAudioManager audioManager;
        private IInputManager inputManager;
        private IPhysicsManager physicsManager;
        private IRenderManager renderManager;
        private ISceneManager sceneManager;
        private Camera2D camera;
        private Game game;


        public GameEngine(Game game)
        {
            this.game = game;
            inputManager = new InputManager(game);
            renderManager = new RenderManager(game);
            sceneManager = new SceneManager(game);
            physicsManager = new PhysicsManager(game, sceneManager);
            game.Services.AddService(typeof(IPhysicsManager), physicsManager);
            game.Services.AddService(typeof(ISceneManager), sceneManager);
            audioManager = new AudioManager(game);
            game.Services.AddService(typeof(IAudioManager), audioManager);
            game.Services.AddService(typeof(IInputManager), inputManager);
            sceneManager.Initialize();
            camera = new Camera2D(game, game.GraphicsDevice);
            camera.Pos = new Vector2(game.GraphicsDevice.Viewport.Width / 2, game.GraphicsDevice.Viewport.Height / 2);
            camera.Zoom = 2f;
            game.Services.AddService(typeof(Camera2D), camera);
        }


        public Camera2D Camera
        {
            get
            {
                return camera;
            }
            set
            {
                camera = value;
                game.Services.AddService(typeof(Camera2D), camera);
            }
        }


        public ISceneManager SceneManager
        {
            get
            {
                return sceneManager;
            }
            set
            {
                sceneManager = value;
                game.Services.AddService(typeof(ISceneManager), sceneManager);
            }
        }

        public IRenderManager RenderManager
        {
            get
            {
                return renderManager;
            }
            set
            {
                renderManager = value;
                game.Services.AddService(typeof(IRenderManager), renderManager);
            }
        }


        public IPhysicsManager PhysicsManager
        {
            get
            {
                return physicsManager;
            }
            set
            {
                physicsManager = value;
                game.Services.AddService(typeof(IPhysicsManager), physicsManager);
            }
        }


        public IInputManager InputManager
        {
            get
            {
                return inputManager;
            }
            set
            {
                inputManager = value;
                game.Services.AddService(typeof(IInputManager), inputManager);
            }
        }


        public IAudioManager AudioManager
        {
            get
            {
                return audioManager;
            }
            set
            {
                audioManager = value;
                game.Services.AddService(typeof(IAudioManager), audioManager);
            }
        }



        public void PushScene(Scene scene)
        {
            sceneManager.PushScene(scene);
        }

        public void Update(GameTime gameTime)
        {
            inputManager.Update(gameTime);
            sceneManager.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            renderManager.Draw(gameTime, sceneManager.GetActiveScenes());
        }
    }
}
